<?php 

class getshop extends SystemAction {

	//ajax
	public function lottery_shop_json(){
		$this->lottery_yjshop_json();
		//没有gid直接返回错误
		if(!isset($_GET['gid'])){
			echo json_encode(array("error"=>'1'));
			return;exit;
		}
		//第一次传入gid为空
		$gid  = trim($_GET['gid']);
		//开奖动画时间
		$times = (int)System::load_sys_config('system','goods_end_time');
		if(!$times){$times = 1;}	
		
		$db = System::load_sys_class('model');
		$gid = safe_replace($gid);
		$gid = str_ireplace("select","",$gid);
		$gid = str_ireplace("union","",$gid);	
		$gid = str_ireplace("'","",$gid);	
		$gid = str_ireplace("%27","",$gid);
		$gid  = trim($gid,',');
		
		//没有gid 获取最近的一条要揭晓的商品
		if(!$gid){
			$info = $db->GetOne("select qishu,xsjx_time,id,zongrenshu,thumb,title,q_uid,q_user,q_user_code,q_end_time from `@#_shoplist` where `q_showtime` = 'Y' order by `q_end_time` ASC");
		}else{
		//有gid获取所有要揭晓的商品		
			$infos = $db->GetList("select  qishu,xsjx_time,id,zongrenshu,thumb,title,q_uid,q_user,q_user_code,q_end_time from `@#_shoplist` where `q_showtime` = 'Y' order by `q_end_time` ASC limit 0,4");			
			$gid = @explode('_',$gid);		
			$info = false;		
			foreach($infos as $infov){
				if(!in_array($infov['id'],$gid)){
					$info = $infov;
					break;
				}
			}			
		}
		
		if(!$info){
			echo json_encode(array("error"=>'1'));
			return;exit;
		}

		//限时揭晓，结果时间加一个动画时间
		if($info['xsjx_time']){$info['q_end_time'] = $info['q_end_time']+$times;}
		//获取中奖人用户名
		System::load_sys_fun("user");
		$user = unserialize($info['q_user']);
		$user = get_user_name($info['q_uid'],"username");
		$uid  = $info['q_uid'];
		$upload = G_UPLOAD_PATH;
		//揭晓时间，精确到秒
		$q_time = substr($info['q_end_time'],0,10);
		//改变正在揭晓的商品状态为已揭晓   info里的id换成外面的gid
		if($q_time <= time()){
			$db->Query("update `@#_shoplist` SET `q_showtime` = 'N' where `id` = '$info[id]' and `q_showtime` = 'Y' and `q_uid` is not null");
			echo json_encode(array("error"=>'-1'));			
			return;	exit;
		}	
		$user_shop_number = $db->GetOne("select sum(gonumber) as gonumber from `@#_member_go_record` where `uid`= '$uid' and `shopid` = '$info[id]' and `shopqishu` = '$info[qishu]'");
		$user_shop_number= $user_shop_number['gonumber'];
		$times = $q_time-time();	
		echo json_encode(array("error"=>"0","user_shop_number"=>"$user_shop_number","user"=>"$user","zongrenshu"=>$info['zongrenshu'],"q_user_code"=>$info['q_user_code'],"qishu"=>$info['qishu'],"upload"=>$upload,"thumb"=>$info['thumb'],"id"=>$info['id'],"uid"=>"$uid","title"=>$info['title'],"user"=>$user,"times"=>$times));
		exit;
	}

	//ajax 获取并计算出最新的中奖信息  yjmp
	public function lottery_yjshop_json(){
		$db = System::load_sys_class('model');
		//即将揭晓的商品列表
		$now_time = time();
		$shoplist = $db->GetList("select id,qishu,maxqishu,sid from `@#_shoplist` where `shenyurenshu` ='0' and `xsjx_time` < '$now_time' ");

		if (count($shoplist) > 0) {
			$yj_code = System::load_app_class('yjcode','pay');
			$code_info = $yj_code->get_lottery_code();
			//中奖时间的时间戳
			$lottery_time = strtotime($code_info['createdAt']);	
			$lottery_code = str_replace(",","",$code_info['numbers']);
			$lottery_pos = $code_info['issue'];

			//循环抽奖订单，找出有没有人中奖
			//用户id和云购码序列，循环将要揭晓的商品列表
			foreach ($shoplist as $k => $v) {
				$time_sum = $v['q_counttime'];

				//生成中奖号码
				$final_code = $time_sum+$lottery_code;
				$final_code = fmod((float)$final_code,$v['money']) + 1;
				//查询中奖用户信息
				$record_list = $db->GetList(" select * from `@#_member_go_record` where shopid = '$v[id]'");
				$u_go_info = array();
				$record_id = 0;
				foreach ($record_list as $k1 => $v1) {
					$code_arr = explode(',', $v1['goucode']);
					foreach ($code_arr as $k2 => $v2) {
						if ($v2 == $final_code) {
							$u_go_info = $db->GetOne(" select * from `@#_member` where uid = '$v1[uid]'");
							$record_id = $v1['id'];
							break;
						}
					}
				}

				//中奖的用户信息
				$u_info = $db->GetOne("select * from `@#_member` where `uid` = '$u_go_info[uid]'");
				$u_info['username'] = _htmtocode($u_info['username']);
				$q_user = serialize($u_info);
				$q_uid = $u_info['uid'];
				$q_1 = $db->Query("UPDATE `@#_shoplist` SET								
									`q_uid` = '$q_uid',
									`q_user` = '$q_user',
									`q_user_code` = '$final_code',
									`q_end_time` = '$v[xsjx_time]',
									`lot_time` = '$lottery_code',
									`lot_pos` = '$lottery_pos',
									`q_showtime` = 'Y'
									 where `id` = '$id'");
				$q_2 = $db->Query("UPDATE `@#_member_go_record` SET `huode` = '$final_code' where `id` = '$record_id'");
			}
		}			
	}

	//ajax	关闭倒计时
	public function lottery_shop_get(){
		if(isset($_POST['lottery_shop_get'])){
			$db = System::load_sys_class('model');
			$times = (int)System::load_sys_config('system','goods_end_time');
			$gid = isset($_POST['gid']) ? abs(intval($_POST['gid'])) : exit();
			$info = $db->GetOne("select id,xsjx_time,q_end_time from `@#_shoplist` where `id` ='$gid' and `q_showtime` = 'Y'");
			if(!$info){
				echo "no";exit;
			}
			if($info['xsjx_time']){$info['q_end_time'] = $info['q_end_time']+$times;}
			$q_time =  intval(substr($info['q_end_time'],0,10));
			if($q_time <= time()){
				$db->Query("update `@#_shoplist` SET `q_showtime` = 'N' where `id` = '$info[id]' and `q_showtime` = 'Y' ");
			}
			echo $q_time - time();			
			exit;						
		}
	}
	
	//ajax  商品详情页走完倒计时后执行的方法 
	//在这里执行计算抽奖方法
	public function lottery_shop_set(){
		if(isset($_POST['lottery_sub'])){
			$db = System::load_sys_class('model');
			$times = (int)System::load_sys_config('system','goods_end_time');
			$gid = isset($_POST['gid']) ? abs(intval($_POST['gid'])) : exit();
			$info = $db->GetOne("select id,xsjx_time,thumb,title,q_uid,q_user,q_end_time from `@#_shoplist` where `id` ='$gid'");
			
			if(!$info || empty($info['q_end_time'])){
				echo '0';exit;
			}		
			if($info['xsjx_time']){$info['q_end_time'] = $info['q_end_time']+$times;}
			$times =  str_ireplace(".","",$info['q_end_time']);
			$q_time = substr($info['q_end_time'],0,10);
			$q = false;
			if(time() >= $q_time){
				$q = $db->Query("update `@#_shoplist` SET `q_showtime` = 'N' where `id` = '$gid' and `q_showtime` = 'Y' ");	
			}
			if($q)
				echo '1';
			else
				echo '0';
		}
	}

}

?>